package com.proyecto.xpdroid;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.xmlpull.v1.XmlPullParserException;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.droid.users.util.GastoHolder;
import com.droid.users.util.UserHolder;
import com.droid.users.util.XPDroidConstants;
import com.proyecto.xpdroid.dummy.DummyContent;
import com.proyecto.xpdroid.http.SimpleHttpClient;
import com.proyecto.xpdroid.util.XmlGast;
import com.proyecto.xpdroid.util.XmlHolder;
import com.proyecto.xpdroid.util.XmlObj;
import com.proyecto.xpdroid.util.XmlReco;

/**
 * A fragment representing a single Item detail screen. This fragment is either
 * contained in a {@link ItemListActivity} in two-pane mode (on tablets) or a
 * {@link ItemDetailActivity} on handsets.
 */
public class ItemDetailFragment extends Fragment {
	/**
	 * The fragment argument representing the item ID that this fragment
	 * represents.
	 */
	public static final String ARG_ITEM_ID = "item_id";
	public static String email = "email";

	/**
	 * The dummy content this fragment is presenting.
	 */
	private DummyContent.DummyItem mItem;

	/**
	 * Mandatory empty constructor for the fragment manager to instantiate the
	 * fragment (e.g. upon screen orientation changes).
	 */
	public ItemDetailFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (getArguments().containsKey(ARG_ITEM_ID)) {
			// Load the dummy content specified by the fragment
			// arguments. In a real-world scenario, use a Loader
			// to load content from a content provider.
			mItem = DummyContent.ITEM_MAP.get(getArguments().getString(
					ARG_ITEM_ID));
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_item_detail,
				container, false);

		// Show the dummy content as text in a TextView.
		if (mItem != null) {

			Context ct = this.getActivity().getApplicationContext();
			UserHolder us = (UserHolder) ct;

			if (ct != null) {
				email = us.getEmail();
			}

			GastoHolder gh = new GastoHolder();

			if (mItem.id.equalsIgnoreCase("1")) {
				String resp = gh.getObjetivosWeb(email);
				XmlObj xmlstring= new XmlObj();
				try {
					resp=xmlstring.getXML(resp);
				} catch (XmlPullParserException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				((TextView) rootView.findViewById(R.id.item_detail))
						.setText(resp.toString());

			}
			if (mItem.id.equalsIgnoreCase("2")) {
				String resp = gh.getGastosWeb(email);
				
				XmlGast xmlstring= new XmlGast();
				try {
					resp=xmlstring.getXML(resp);
				} catch (XmlPullParserException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				((TextView) rootView.findViewById(R.id.item_detail))
						.setText(resp.toString());

			} 
			if (mItem.id.equalsIgnoreCase("3")) {
				String resp = gh.getRegistrosWeb(email);
				
				XmlReco xmlstring= new XmlReco();
				try {
					resp=xmlstring.getXML(resp);
				} catch (XmlPullParserException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				((TextView) rootView.findViewById(R.id.item_detail))
						.setText(resp.toString());

			} 

			//			else {
//				((TextView) rootView.findViewById(R.id.item_detail))
//						.setText(mItem.content);
//			}

		}

		return rootView;
	}

}
